package com.example.payment.service.impl;

import com.example.common.enums.ResultCode;
import com.example.common.exception.ServiceException;
import com.example.common.req.payment.PaymentReq;
import com.example.common.resp.payment.PaymentResp;
import com.example.common.util.ConvertUtil;
import com.example.payment.entity.Payment;
import com.example.payment.repository.PaymentRepository;
import com.example.payment.service.PaymentService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
public class PaymentServiceImpl implements PaymentService {

    @Resource
    private PaymentRepository paymentRepository;

    @Override
    public Long create(PaymentReq req) {
        Payment payment = ConvertUtil.convert(req, Payment.class);
        ConvertUtil.initInsertEntity(payment);
        return paymentRepository.save(payment).getId();
    }

    @Override
    public PaymentResp get(Long id) {
        Payment payment = paymentRepository.findById(id)
                .orElseThrow(() -> new ServiceException(ResultCode.CMN_RESOURCE_NOT_FOUND));
        return ConvertUtil.convert(payment, PaymentResp.class);
    }
}
